using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


using Material.Object;
using Material.DAL;
namespace Material.Entity
{
	public class TblTransPembBarangHDREntity
	{
		private Accessor accessor;
		private string connString;

		public TblTransPembBarangHDREntity (string _connString)
		{
			 this.connString = _connString;
		}

		public int Insert(TblTransPembBarangHDR tbltranspembbaranghdr)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransPembBarangHDR_Insert";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add("@IdTrans" ,tbltranspembbaranghdr.IdTrans );
			 accessor.Add("@IdBarang" ,tbltranspembbaranghdr.IdBarang );
			 accessor.Add("@CreatedDate" ,tbltranspembbaranghdr.Created );
			 accessor.Add("@CreatedBy" ,tbltranspembbaranghdr.CreatedBy );
             accessor.Add("@ModifiedDate", tbltranspembbaranghdr.Modified);
			 accessor.Add("@ModifiedBy" ,tbltranspembbaranghdr.ModifiedBy );

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public int Update(TblTransPembBarangHDR tbltranspembbaranghdr)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransPembBarangHDR_Update";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"IdTrans" ,tbltranspembbaranghdr.IdTrans );
			 accessor.Add(@"IdBarang" ,tbltranspembbaranghdr.IdBarang );
			 accessor.Add(@"CreatedDate" ,tbltranspembbaranghdr.Created );
			 accessor.Add(@"CreatedBy" ,tbltranspembbaranghdr.CreatedBy );
             accessor.Add(@"ModifiedDate", tbltranspembbaranghdr.Modified);
			 accessor.Add(@"ModifiedBy" ,tbltranspembbaranghdr.ModifiedBy );

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public int Delete(TblTransPembBarangHDR tbltranspembbaranghdr)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransPembBarangHDR_Delete";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"IdTrans" ,tbltranspembbaranghdr.IdTrans);

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public TblTransPembBarangHDR GetDataByID(TblTransPembBarangHDR tbltranspembbaranghdr)
		{
			 TblTransPembBarangHDR result = new TblTransPembBarangHDR();
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransPembBarangHDR_SelectByID";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"IdTrans" ,tbltranspembbaranghdr.IdTrans);

			 SqlDataReader rdr = Excutor.GetDataReader(accessor);
			 if (rdr.HasRows)
			 {
				 while (rdr.Read())
					 {
						 result.IdTrans = rdr.GetInt32(0);
						 result.IdBarang = rdr.GetInt32(1);
						 result.Created = rdr.GetDateTime(2);
						 result.CreatedBy = rdr.GetString(3);
						 result.Modified = rdr.GetDateTime(4);
						 result.ModifiedBy = rdr.GetString(5);
					  }
			}
			 return result;
		}

        public TblTransPembBarangHDR GetMaxIDTrans()
        {
            TblTransPembBarangHDR result = new TblTransPembBarangHDR();
            accessor = new Accessor(true, connString);
            accessor.CommandText = "TblTransPembBarangHDR_GetMaxID";
            accessor.CommandType = CommandType.StoredProcedure;
            //accessor.Add(@"IdTrans", tbltranspembbaranghdr.IdTrans);

            DataSet ds   = Excutor.GetDataSet(accessor);
            if (ds.Tables[0].Rows.Count>0)
            {

                result.IdTrans = Convert.ToInt32(ds.Tables[0].Rows[0][0]) + 1;
                
            }
            else
                result.IdTrans = 1;
            return result;
        }

		public List<TblTransPembBarangHDR> GetDataAll()
		{
			 List<TblTransPembBarangHDR> result = new List<TblTransPembBarangHDR>();
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransPembBarangHDR_SelectAll";
			 accessor.CommandType = CommandType.StoredProcedure;

			 SqlDataReader rdr = Excutor.GetDataReader(accessor);
			 if (rdr.HasRows)
			 {
				 while (rdr.Read())
					 {
						 TblTransPembBarangHDR obj = new TblTransPembBarangHDR();
						 obj.IdTrans = rdr.GetInt32(0);
						 obj.IdBarang = rdr.GetInt32(1);
						 obj.Created = rdr.GetDateTime(2);
						 obj.CreatedBy = rdr.GetString(3);
						 obj.Modified = rdr.GetDateTime(4);
						 obj.ModifiedBy = rdr.GetString(5);
						 result.Add(obj);
					  }
			}
			 return result;
		}

        public TblTransPembBarangHDR GetDataByIDBarang(TblTransPembBarangHDR tbltranspembbaranghdr)
        {
            TblTransPembBarangHDR result = new TblTransPembBarangHDR();
            accessor = new Accessor(true, connString);
            accessor.CommandText = "TblTransPembBarangHDR_SelectByIDBarang";
            accessor.CommandType = CommandType.StoredProcedure;
            accessor.Add(@"IdBarang", tbltranspembbaranghdr.IdBarang);

            SqlDataReader rdr = Excutor.GetDataReader(accessor);
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    result.IdTrans = rdr.GetInt32(0);
                    result.IdBarang = rdr.GetInt32(1);
                    result.Created = rdr.GetDateTime(2);
                    result.CreatedBy = rdr.GetString(3);
                    result.Modified = rdr.GetDateTime(4);
                    result.ModifiedBy = rdr.GetString(5);
                }
            }
            return result;
        }


	}
}
